<template>
    <div class="login">
        <div class="login-container">
            <h1>系统登录</h1>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                <!-- 用户框 -->
                <el-form-item label="" prop="username">
                    <el-input type="text" prefix-icon="el-icon-s-custom" clearable v-model="ruleForm.pass" autocomplete="off" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <!-- /用户框 -->

                <!-- 密码框 -->
                <el-form-item label="" prop="password" >
                    <el-input type="password" prefix-icon="el-icon-lock" clearable v-model="ruleForm.checkPass" autocomplete="off" placeholder="请输入密码" show-password></el-input>
                </el-form-item>
                <!-- /密码框 -->
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
 export default {
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年龄不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value < 18) {
              callback(new Error('必须年满18岁'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          checkPass: '',
          age: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped lang="scss">
//登录页面
.login{
    width: 100%;
    height: 100%;
    background-color: #2D3A4B;
    overflow: hidden;
    //登录框
    .login-container{
        width: 450px;
        margin: 200px auto ;
        h1{
            padding-bottom: 30px ;
            font-size: 26px;
            color: #eee;
            font-weight: 600;
        }
        .el-input{
            width: 100%;
            background: #2D3A4B;
            margin: 5px 0;
            
        }
        .el-button{
            width: 100%;
            margin-top: 5px;
        }
        
    }
}
</style>
